package com.ruoyi.web.controller.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.web.controller.system.domain.category.Product;
import com.ruoyi.web.controller.system.domain.category.oneCategory;
import com.ruoyi.web.controller.system.mapper.ProductMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.controller.system.mapper.CategoryMapper;
import com.ruoyi.web.controller.system.domain.Category;
import com.ruoyi.web.controller.system.service.ICategoryService;

import javax.security.auth.Subject;

/**
 * 菜单Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-12-21
 */
@Service
public class CategoryServiceImpl implements ICategoryService 
{
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询菜单
     * 
     * @param id 菜单ID
     * @return 菜单
     */
    @Override
    public Category selectCategoryById(Long id)
    {
        return categoryMapper.selectCategoryById(id);
    }

    /**
     * 查询菜单列表
     * 
     * @param category 菜单
     * @return 菜单
     */
    @Override
    public List<Category> selectCategoryList(Category category)
    {
        return categoryMapper.selectCategoryList(category);
    }

    /**
     * 新增菜单
     * 
     * @param category 菜单
     * @return 结果
     */
    @Override
    public int insertCategory(Category category)
    {
        return categoryMapper.insertCategory(category);
    }

    /**
     * 修改菜单
     * 
     * @param category 菜单
     * @return 结果
     */
    @Override
    public int updateCategory(Category category)
    {
        return categoryMapper.updateCategory(category);
    }

    /**
     * 批量删除菜单
     * 
     * @param ids 需要删除的菜单ID
     * @return 结果
     */
    @Override
    public int deleteCategoryByIds(Long[] ids)
    {
        return categoryMapper.deleteCategoryByIds(ids);
    }

    /**
     * 删除菜单信息
     * 
     * @param id 菜单ID
     * @return 结果
     */
    @Override
    public int deleteCategoryById(Long id)
    {
        return categoryMapper.deleteCategoryById(id);
    }

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<oneCategory> getAllCategory(Category c) {
        //最终要的数据列表
        ArrayList<oneCategory> categoryList = new ArrayList<>();

        //获取一级分类数据记录
        /*QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",0);
        queryWrapper.orderByAsc("sort","id");*/
        List<Category> category = categoryMapper.selectCategoryList(c);

        //获取二级分类数据记录
       /* QueryWrapper<Subject> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.ne("parent_id",0);
        queryWrapper1.orderByAsc("sort","id");*/
        List<Product> product = productMapper.selectProductList(new Product());

        //填充一级分类数据
        int count=category.size();
        for(int i=0;i<count;i++){
            Category category1 = category.get(i);

            //创建一级分类级别对象
            oneCategory oneCategory = new oneCategory();
            BeanUtils.copyProperties(category1,oneCategory);
            categoryList.add(oneCategory);

            //填充二级分类数据
            ArrayList<Product> productList = new ArrayList<>();
            int count2=product.size();
            for(int j=0;j<count2;j++){
                Product product1 = product.get(j);
                if(category1.getId().equals(product1.getCategoryid())){
                    //创建二级类别对象
                    Product product2 = new Product();
                    BeanUtils.copyProperties(product1,product2);
                    productList.add(product2);
                }
            }
            oneCategory.setChildren(productList);
        }
        return categoryList;
    }
}
